home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 19 / envi.zip / ENVIRONM.INC < prev    next >
Text File  |  1986-03-29  |  10KB  |  237 lines

  1. {ENVIRONM.INC :
  2.    This provides the Boolean function FoundInEnv(WordIn, var WordOut) which 
  3.  searches the DOS Environment. WordIn is either a string which may be found in 
  4.  the Environment (eg, PATH) or the null string '', in which case an attempt will 
  5.  be made to determine, from the Environment, in which directory the program 
  6.  being executed resides.  
  7.  
  8.    If the DOS is 1.X, FoundInEnv returns False, with WordOut unchanged.
  9.  
  10.    If the DOS is 2.X, FoundInEnv returns:
  11.  
  12.                                          False, with WordOut unchanged, if 
  13.  WordIn is '', or if WordIn is not found in the Environment.  
  14.                                          True, with WordIn changed to the 
  15.  Environment string = to WordIn, if WordIn is found in the Environment. (Eg, 
  16.  FoundInEnv('foo',WordOut) returns True with WordOut = 'nothing much' if the 
  17.  Environment contains the string "FOO=nothing much".) 
  18.  
  19.    If the DOS is 3.X, FoundInEnv returns:
  20.  
  21.                                          the same as for DOS 2.X, except:
  22.                                          True, with WordOut changed to the
  23.  path to the directory in which resides the program being executed, and the file 
  24.  name of the program, if WordIn is ''. Eg, FoundInEnv returns True with WordOut 
  25.  = "C:\TURBO\ENVIRONM.COM" if the executed program is ENVIRONM.COM, located in
  26.  subdirectory \TURBO on disk C:. (It is worth noting that if a program is
  27.  compiled in memory and run, a call of if FoundInEnv('',WordOut) with DOS 3.X
  28.  will resultin WordOut being set to "<d:>[path]TURBO.COM", etc. The program
  29.  compiled and run in memory has no environment space of its own, but accesses
  30.  the environment which DOS has handed to the compiler.)
  31.  }
  32.  
  33. {The file DOS.INC must be included prior to the inclusion of ENVIRONM.INC.}
  34.  
  35. {type BigString must precede the function declaration because it is used as
  36.  the type of FoundInEnv's parameters. Other variables used as the actual
  37.  parameters do not have to be of the same type, altho they should be
  38.  string[255]. If the compiler director $V is turned off, the actual parameters
  39.  can be shorter types; this is dangerous if the value to be returned thru
  40.  WordOut is longer than the declared length of WordOut: other data may then
  41.  be overwritten.}
  42.  
  43. TYPE
  44.   BigString = STRING[255];
  45.  
  46.   FUNCTION FoundInEnv(WordIn : BigString; VAR WordOut : BigString) : Boolean;
  47.  
  48.   CONST
  49.     ESTM : Char = ^@;         {Environment strings are null-terminated.}
  50.     EEM : STRING[2] = ^@^@;   {marker for end of Environment}
  51.  
  52.   VAR
  53.     MaxStr : BigString;       {MaxStr is the target string during the search}
  54.     SEnv : Integer ABSOLUTE CSeg : $2C; {SEnv is the segment of Environment}
  55.     OEnv : Integer;           {OEnv is the offset within Environment}
  56.     EEPos : Integer;          {offset of end of Environment marker}
  57.     WPos : Integer;           {offset of Word, if it is found}
  58.     EST : Integer;            {offset of end of Word string}
  59.  
  60. { If an Environment variable corresponding to Word is found, FoundWord
  61.   returns True, with SetTo set to the value of Word in the Environment.
  62.   Otherwise, FoundWord returns False, with SetTo unchanged. }
  63.  
  64.     FUNCTION FoundWord(VAR Word, SetTo : BigString) : Boolean;
  65.     VAR
  66.       LW : Integer;           {length of Word}
  67.       I : Integer;            {general counter}
  68.       Found,                  {True if we find Word}
  69.       Done : Boolean;         {True if we run out of Environment space}
  70.     BEGIN                     {FoundWord}
  71.  
  72. { In the Environment, Word will be WORD= }
  73.  
  74.       Word := Word+'=';
  75.       LW := Length(Word);
  76.       FOR I := 1 TO LW - 1 DO Word[I] := UpCase(Word[I]);
  77.  
  78. { We haven't found or done anything yet. }
  79.  
  80.       Found := False;
  81.       Done := False;
  82.  
  83. { We will start at the beginning of the Environment space. }
  84.       OEnv := 0;
  85.  
  86. { We don't care what's in MaxStr right now, just that it acts like it's
  87.   got 255 chars in it. }
  88.  
  89.       MaxStr[0] := Char(255);
  90.       WHILE NOT(Done OR Found) DO BEGIN
  91.  
  92. { If "WORD=" is the first thing in the Environment, then that's what we'll
  93.   be trying to match. It it is further into the environment, then we will
  94.   match "^@WORD=", where ^@ is a byte of 0. }
  95.  
  96.         IF (OEnv <> 0) AND (Word[1] <> ^@) THEN BEGIN
  97.           Word := ^@+Word;
  98.           LW := LW+1;
  99.         END;                  {IF (OEnv <> 0) AND (Word[1] <> ^@)}
  100.  
  101. { Copy 255 bytes of the Environment space into MaxStr. }
  102.  
  103.         Move(Mem[SEnv:OEnv], MaxStr[1], 255);
  104.  
  105. { If we find EEPos in those 255 bytes, then we've found the end of the
  106.   Environment space, so Done will be true and we must shorten MaxStr. }
  107.  
  108.         EEPos := Pos(EEM, MaxStr);
  109.         IF EEPos <> 0 THEN BEGIN
  110.           Done := True;
  111.           MaxStr[0] := Char(EEPos);
  112.         END;                  {if EEPos<>0}
  113.  
  114. { I let the standard Turbo function Pos do the searching for me. That's
  115.   a lazy trade-off: It was easier for me to write and debug this search
  116.   routine than to write one using a search which would extend more than
  117.   255 bytes. Searching the typical DOS Environment, I think there's little
  118.   value to be gained from a faster search, anyway. }
  119.  
  120. { It we find Word in those 255 (or less) bytes, then we have found
  121.   what we're looking for, IF:
  122.         we are at the start of the Environment space [OEnv = 0] AND
  123.   Word is the very first thing there;
  124.         OR we are in the first pass thru here [OEnv = 0] AND Word is
  125.   preceded by ^@;
  126.         OR we are in a subsequent pass thru here [OEnv<>0 AND we have put
  127.   ^@ on the front of Word ourselves. }
  128.  
  129.         WPos := Pos(Word, MaxStr);
  130.         IF WPos <> 0 THEN
  131.           IF (OEnv <> 0) OR ((OEnv = 0) AND ((WPos = 1) OR
  132.           (MaxStr[WPos-1] = Char(0))))
  133.           THEN Found := True;
  134.  
  135. { If we haven't found Word or reached the end of the Environment space, then
  136.   we'll offset (255 - the length of Word - 1) bytes further into the
  137.   Environment space. (The funny increment is in case Word is on the boundary
  138.   between two 255 byte blocks.) }
  139.  
  140.         IF NOT(Done OR Found) THEN
  141.           OEnv := OEnv+255-(LW-1);
  142.       END;                    {while not (Done or Found)}
  143.  
  144. { If we have found Word, we'll set the offset to the first byte after "WORD=".
  145.   We don't know how long the string will be, but is should be less that 255
  146.   bytes. I really don't know for certain that an Environment string can't be
  147.   longer than that--I just know that you can't enter that long a string from the 
  148.   keyboard or a batch file, and I don't think the string is likely to be entered 
  149.   any other way. If you do--or if that possibility worries you, you're free to 
  150.   modify this routine accordingly. }
  151.  
  152.       IF Found THEN BEGIN
  153.         OEnv := OEnv+WPos+LW-1;
  154.         Move(Mem[SEnv:OEnv], MaxStr[1], 255);
  155.         MaxStr[0] := Char(255);
  156.  
  157. { So I'm saying that if the end-of-string marker is not in the first 255 bytes,
  158.   then we haven't found a value for Word. If it is, then we'll copy all of
  159.   the string up to ESTM to SetTo. }
  160.  
  161.         EST := Pos(ESTM, MaxStr);
  162.         IF EST <> 0 THEN SetTo := Copy(MaxStr, 1, EST-1)
  163.         ELSE Found := False;
  164.       END;                    {if Found}
  165.       FoundWord := Found;
  166.     END;                      {FoundWord}
  167.  
  168. { What I call "Home" is found immediately AFTER the Environment space
  169.   with DOS 3.X. Since it's AFTER, maybe it should have a routine all to
  170.   itself. In my mine, tho, it's part of the Environment. "Home" consists
  171.   of a drive specifier, the path to the directory where our program is
  172.   located, and the name of the program as DOS knows it. }
  173.  
  174.  
  175.     FUNCTION FoundHome(VAR PathHome : BigString) : Boolean;
  176.     BEGIN                     {FoundHome}
  177.  
  178. { Unfortunately, DOS 2.X doesn't have this information. }
  179.  
  180.       IF DOS < 3.0 THEN FoundHome := False
  181.       ELSE BEGIN              {DOS>=3.0}
  182.  
  183. { We'll use MaxStr here just as we did above, but we'll be searching
  184.   for a "word" of two bytes of 0, which will mark the end of the
  185.   formal Environment space. Understanding what we're looking for here,
  186.   the comments above should clarify the first part of this code. Note that,
  187.   here, we are not Done when we reach the EEM--we've just gotten to where
  188.   we were going. }
  189.  
  190.         MaxStr[0] := Char(255);
  191.         EEPos := 0;
  192.         OEnv := 0;
  193.         REPEAT                {until EEPos<>0}
  194.           Move(Mem[SEnv:OEnv], MaxStr[1], 255);
  195.           EEPos := Pos(EEM, MaxStr);
  196.           IF EEPos = 0 THEN OEnv := OEnv+244;
  197.         UNTIL EEPos <> 0;
  198.  
  199. { Set the offset to the first byte after the end-of-environment marker.
  200.   Then we reveal the same conditions as above: If the end-of-string marker
  201.   isn't in the next 255 bytes then we give up. But that condition isn't a
  202.   limitation here--this string is limited to 79 characters. }
  203.  
  204.         OEnv := OEnv+EEPos+3;
  205.         Move(Mem[SEnv:OEnv], MaxStr[1], 255);
  206.         EST := Pos(ESTM, MaxStr);
  207.         IF EST = 0 THEN FoundHome := False
  208.         ELSE BEGIN            {EST<>0}
  209.           FoundHome := True;
  210.           PathHome := Copy(MaxStr, 1, EST-1);
  211.         END;                  {else EST<>0}
  212.       END;                    {DOS>=3.0}
  213.     END;                      {FoundHome}
  214.  
  215.   BEGIN                       {FoundInEnv}
  216.  
  217. { If we're running DOS 1.X [or 0.X?] then there is no Environment space. }
  218.  
  219.     IF DOS < 2.0 THEN FoundInEnv := False
  220.  
  221. { Otherwise, if WordIn = '', that means we want to find "Home". }
  222.  
  223.     ELSE IF WordIn = '' THEN FoundInEnv := FoundHome(WordOut)
  224.  
  225. { Else WordIn = something, and we want to search the Environment space for
  226.   that something. }
  227.  
  228.     ELSE FoundInEnv := FoundWord(WordIn, WordOut);
  229.   END;                        {FoundInEnv}
  230.  
  231.  
  232. { All of this horribly over-commented code, which should ARC admirably,
  233.   is the product of
  234.                     Karl Brendel, CIS 73307,3101
  235.                     718 East B Avenue
  236.                     Hutchinson, Kansas  67501 }
  237.